#include "setwig.h"
#include "ui_setwig.h"
#include "qmqtt_message.h"
#include "globalval.h"
#include <QTimer>
#include <qmqtt_client.h>
#include <QByteArray>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QMutex>
#include <QStandardPaths>
#include <QUuid>
#include "newdialog.h"
#include "netdia.h"
#include "aboutdia.h"
#include "logdia.h"
SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    ui->setupUi(this);
}

SetWig::~SetWig()
{
    delete ui;
}



void SetWig::on_notBtn_clicked()
{
    //不上报
    QJsonObject dataObj;
    dataObj.insert("type",0);//0-不上报，客户端主动采集；1-变化上报，即连续2次值不相等；2-周期上报
    dataObj.insert("period","5");
    QJsonObject allObj;
    allObj.insert("data",dataObj);
    allObj.insert("type",3);
    // 序列化为JSON文档
    QJsonDocument doc(allObj);
    QByteArray data = doc.toJson(QJsonDocument::Compact);

    QMQTT::Message message;
    message.setTopic("/app/data/down");
    message.setPayload(data);
    qDebug() << data;
    GlobalVal::mqtt->publish(message);
}

void SetWig::on_chaBtn_clicked()
{
    //变化上报
    QJsonObject dataObj;
    dataObj.insert("type",1);//0-不上报，客户端主动采集；1-变化上报，即连续2次值不相等；2-周期上报
    dataObj.insert("period","5");
    QJsonObject allObj;
    allObj.insert("data",dataObj);
    allObj.insert("type",3);
    // 序列化为JSON文档
    QJsonDocument doc(allObj);
    QByteArray data = doc.toJson(QJsonDocument::Compact);

    QMQTT::Message message;
    message.setTopic("/app/data/down");
    message.setPayload(data);
    qDebug() << data;
    GlobalVal::mqtt->publish(message);
}

void SetWig::on_perBtn_clicked()
{
    QString period = ui->perEdit->toPlainText();
    if (period.isEmpty()) {
        qDebug() << "输入的周期为空";
        QMessageBox::critical(this,"错误", "输入周期为空！！");
        ui->perBtn->setCheckable(false);
        return;
    }

    //周期上报
    QJsonObject dataObj;
    dataObj.insert("type",2);//0-不上报，客户端主动采集；1-变化上报，即连续2次值不相等；2-周期上报
    dataObj.insert("period",period.toInt());
    QJsonObject allObj;
    allObj.insert("data",dataObj);
    allObj.insert("type",3);
    // 序列化为JSON文档
    QJsonDocument doc(allObj);
    QByteArray data = doc.toJson(QJsonDocument::Compact);

    QMQTT::Message message;
    message.setTopic("/app/data/down");
    message.setPayload(data);
    qDebug() << data;
    GlobalVal::mqtt->publish(message);
}

void SetWig::on_pushButton_clicked()//设备升级
{
    NewDialog *updata=new NewDialog(this);
    updata->show();
}

void SetWig::on_pushButton_2_clicked()
{
    //跳转连接界面
    netDia *w = new netDia();
    w->show();
}

void SetWig::on_pushButton_3_clicked()
{
    aboutDia *about = new aboutDia();
    about->show();
}

